package com.pxst.merchant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pxst.merchant.entity.SysMerchantAvailableBankEntity;
import com.pxst.merchant.mapper.SysMerchantAvailableBankMapper;
import com.pxst.merchant.mapper.SysMerchantInfoMapper;
import com.pxst.merchant.service.SysMerchantAvailableBankService;
import com.pxst.resp.ApiResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: @Ryan
 * @Description:
 * @CreateTime: 2023-09-13 16:10
 */
@Slf4j
@Service
public class SysMerchantAvailableBankServiceImpl implements SysMerchantAvailableBankService {
    @Autowired
    private SysMerchantAvailableBankMapper sysMerchantAvailableBankMapper;

    @Autowired
    private SysMerchantInfoMapper sysMerchantInfoMapper;

    @Override
    public ApiResp<String> addSysMerchantAvailableBank(SysMerchantAvailableBankEntity avail) {

        // 判断是否存在
        QueryWrapper<SysMerchantAvailableBankEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("merchant_id",avail.getMerchantId());
        wrapper.eq("available_bank_id",avail.getAvailableBankId());
        List<SysMerchantAvailableBankEntity> list = sysMerchantAvailableBankMapper.selectList(wrapper);
        if (!list.isEmpty()){
            return ApiResp.paramError("商户该银行已存在");
        }
        sysMerchantAvailableBankMapper.insert(avail);
        return ApiResp.sucess();
    }

    @Override
    public ApiResp<String> updateSysMerchantAvailableBank(SysMerchantAvailableBankEntity avail) {
        // 只允许修改可用状态
        SysMerchantAvailableBankEntity existAvail = sysMerchantAvailableBankMapper.selectById(avail.getId());
        if (existAvail == null){
            return ApiResp.paramError("该商户不存该银行的可用信息");
        }

        existAvail.setEnabled(avail.getEnabled());
        existAvail.setMessage(avail.getMessage());

        return ApiResp.sucess(sysMerchantAvailableBankMapper.updateById(existAvail));
    }

    @Override
    public ApiResp<List<SysMerchantAvailableBankEntity>> getMerchantAvailableBankListByMerchantId(Long merchantId) {
        // 判断商户是否存在

        if (sysMerchantInfoMapper.selectById(merchantId) == null){
            return ApiResp.paramError("该商户不存在");
        }
        QueryWrapper<SysMerchantAvailableBankEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("merchant_id",merchantId);
        return ApiResp.sucess(sysMerchantAvailableBankMapper.selectList(wrapper));
    }
}
